An error free program?
By Grant Kwai
Copyright (c) 1991 Apple Users' Group, Sydney
Republished from Applecations, a publication of the Apple Users' Group, Sydney, Australia.
You have finally finished that fantastic, brand spanking new program which you have been working on for the past several months. You have laboriously ironed out all those kinks which we so often call a bug or error and anticipate what all of your friends are going to say when they see this creation. All goes well and you have them all in awe. You brag about how this program is absolutely bug free and you tell your friend to press anything; they press Ctrl-C and your Apple // spurts out a "SYNTAX ERROR". Arghh!!
It's times like this where you don't want your program to 'crash' you back to the DOS prompt or into machine code ('*' prompt). Luckily there are ways around this.
The reasons why your Apple gives you a "SYNTAX ERROR", "FORMULA TO COMPLEX", "STRING TO LONG" or several other error messages is because Applesoft has encountered an error on your part (programming typographical errors etc) or an error on the users part (e.g hitting the wrong key/s). These errors are all well and good when you are initially writing and debugging the program because you can see exactly where your program 'bombed' out at. This can make for an easy debugging tool.
However, it is only when you have modified the program to your satisfaction then you still get these same errors because of something you had not anticipated on behalf of the user that it can get rather annoying. As a user, they too get rather annoyed. For such a problem, Applesoft recognizes this command:
ONERR GOTO XXX
where XXX is the particular line number. The purpose of this error trap is to prevent such a situation as described above occurring. What basically happens is that upon an illegal error, the program, instead of stopping and telling you of an error, will ON an ERRor, GO TO the line number desired. For instance:
ONERR GOTO 60000
If an error occurs, the computer program would branch down to line 60000 where you would have your recovery code. For instance, you might inform the program to go back to the beginning and start all over again. Here is an example:
10 ONERR GOTO 60000
20 PRINT "THIS IS AN EXAMPLE OF THE ONERR FUNCTION"
30 PRINT "DO YOU LIKE IT?":INPUT Y$
40 PRINT "YOUR ANSWER WAS ";Y$
50 FOR X=1 TO 1000:NEXT X
60000 GOTO 20
There is nothing wrong with this program. However, what happens if say the user hits Ctrl-C at the loop statement (line 50) for some reason? Normally the computer would take you back to the DOS prompt. However, since you have the ONERR GOTO statement, it will upon this error, go to line 60000. This line then tells the computer to go back to line 20 and do it all again. There is no need to go back to line 10 since the current ONERR GOTO statement will always be in operation.
Instead of looping back, you could also use this command if you want to VERIFY a program in a program. You could issue the verify command and if the file can not be found, the ONERR statement will pick it up and tell the system to go to the line specified. On this line you could then add something like a statement saying 'File not found' then loop back to the main menu.
Obviously there are many more applications for such a versatile command.
I think all of us would want our program to run as if without a hitch even if it doesn't perform the tasks it was supposed to. Though it may stop normal DOS errors from occurring, it does not necessarily mean that there will be no reason as to why it should not have happened.
Upon receiving such an error, you could easily tell the system to PRINT a detailed message as to why their command was illegal (e.g Attempting to divide by zero, file not found on disk etc). You could then ask the user to enter the correct information or insert the correct disk. This not only saves the user much time, but also makes your program appear much more professional and user friendly.
If for some reason though, you want to disable the last ONERR statement, issue the command POKE 216,0 or issue another ONERR command.
Most of us like to share what we create. We can't always anticipate what they are going to do wrong but we can cater for what possibilities that could happen with the use of this command. Most, if not all commercial programs implement an error trap function, so why can't you do the same? You now know how to do it so get cracking!

THIS CONTENT COPYRIGHT © 2007, APPLE MACINTOSH USERS' GROUP, SYDNEY
Permission has been obtained to make this material available on the Internet.

Permission is hereby granted for non-profit user groups to republish this content.
PLEASE CREDIT THE AUTHOR AND THE SOURCE: Applecations, publication of the Apple Users' Group, Sydney, Australia

THIS PAGE COPYRIGHT © 2007, ANDREW ROUGHAN